<?php
namespace app\index\controller;

use app\index\model\Archives;
use app\index\model\Manage_model;
use app\index\model\Organization;
use app\index\model\Storeroom;
use app\index\model\Tag;
use app\index\model\TagArchivesRelation;
use app\index\model\TagGroup;
use think\Controller;
use think\Db;
use think\Request;
use think\Session;

class Manage extends Common {
	protected $request;
	protected $ac_model;
	protected $a_model;
	protected $tag_model;
	protected $tag_group_model;
	protected $tag_archives_relation_model;
	protected $storage_model;
	protected $cate_id;
	protected $org_model;
	protected $org_id;

	public function __construct(Request $request = null) {
		parent::__construct($request);
		$this->request = $request;
		// $this->ac_model = new ArchivesCategory();
		$this->a_model = new Archives();
		$this->tag_model = new Tag();
		$this->tag_archives_relation_model = new TagArchivesRelation();
		$this->tag_group_model = new TagGroup();
		$this->storage_model = new Storeroom();
		$this->org_model = new Organization();
	}
	// 账户管理
	public function index() {
		$this->getAuth();
		Session::delete('org_id');
		// $rev = new Manage_model;
		// $res = $rev->getindex();

		$uid = $_SESSION['think']['username'][0]['user_id'];
		$time = date('Y-m-d H:i:s', time());
		//记录用户操作
		$data = [
			'user_id' => $uid, // 缺少返回user_id
			'log_action' => 'Manage/getindex', //默认2 临时工
			'log_desc' => '浏览账户管理', //默认空
			'create_time' => $time,
			'is_delete' => 0, //默认0
		];
		$res = db('_user_log')
			->insert($data);

		$list = Db::table('cn_user')
			->alias('cu')
			->join('cn_user_character_relation cucr', 'cu.user_id = cucr.user_id')
			->join('cn_character cc', 'cc.chara_id = cucr.chara_id')
			->join('cn_menu_character_relation cmcr', 'cmcr.chara_id = cc.chara_id')
			->join('cn_menu cm', 'cmcr.menu_id = cm.menu_id')
			->where('cn_user.user_id', $uid)
			->field('menu_name')
			->select();
		//查询有哪些角色以及人员 (授权)
		$cha = Db::table('cn_character')
			->field('chara_name,chara_id')
			->where('chara_id', 'neq', 0)
			->where('is_delete',0)
			->order('sort asc')
			->select();
		// var_dump($cha);die;
		// 循环遍历二维数组
		// foreach($cha as $v =>$k){
		//     $character[$k]['chara_name'] = $v['chara_name'];
		//     $character[$k]['chara_id'] = $v['chara_id'];
		// }
		$use = Db::table('cn_user')
			->field('user_name')
			->where('user_name', 'neq', 'admin')
			->where('is_delete', 'neq', '1')
			->select();
		// 循环遍历二维数组
		foreach ($use as $y) {
			$username[] = $y['user_name'];
		}

		// 查询有多少人员  以及对应的角色
		// $user = Db::table('cn_user')
		//         ->field('user_name')
		//         ->where('user_name','neq','admin')
		//         ->select();
		// foreach($user as $z){
		//     $name[] = $z['user_name'];
		// }
		// $a = implode(',', $name);
		// dump($a);die;
		// $all = Db::table('cn_user')
		//         ->alias('cu')
		//         ->join('cn_user_character_relation cucr','cu.user_id = cucr.user_id')
		//         ->join('cn_character cc','cc.chara_id = cucr.chara_id')
		//         ->field('cc.chara_name,cu.user_name')
		//         ->where('cu.user_name','in',$a)
		//         ->where('user_name','neq','admin')
		//         ->where('cc.chara_id','neq',0)
		//         ->where('cu.is_delete','neq','1')
		//         ->where('cc.is_delete','neq','1')
		//         ->where('cucr.is_delete','neq','1')
		//         ->select();
		// 先查询有多少权限
		$all = Db::table('cn_character')
			->where('chara_id', 'neq', 0)
			->where('is_delete', 'eq', 0)
			->field('chara_id,chara_name')
			->order('sort asc')
			->select();
		// 在查询对应权限有多少人
		for ($z = 0; $z < count($all); $z++) {
			$qr[$z] = '';
			// $qx[$z] = Db::table('cn_user_character_relation')
			// 	->alias('cucr')
			// 	->join('cn_user cu', 'cu.user_id = cucr.user_id')
			// 	->join('cn_character cc', 'cc.chara_id = cucr.chara_id')
			// 	->where('cucr.chara_id', $all[$z]['chara_id'])
			// 	->whereOr('cucr.chara_id', 'like', $all[$z]['chara_id'] . ',%')
			// 	->whereOr('cucr.chara_id', 'like', '%,' . $all[$z]['chara_id'])
			// 	->whereOr('cucr.chara_id', 'like', '%,' . $all[$z]['chara_id'] . ',%')
			// 	->where('cu.is_delete', 'eq', 0)
			// 	->where('cc.is_delete', 'eq', 0)
			// 	->field('cu.user_name')
			// 	->select();
			$sql[$z] = "select cu.user_name from cn_user_character_relation as cucr left join cn_user as cu on cu.user_id = cucr.user_id left join cn_character as cc on cc.chara_id = cucr.chara_id where cu.is_delete = 0 and cc.is_delete = 0 and cucr.chara_id = '" . $all[$z]['chara_id'] . "' or cucr.chara_id like '" . $all[$z]['chara_id'] . ',%' . "' or cucr.chara_id like '" . '%,' . $all[$z]['chara_id'] . "' or cucr.chara_id like  '" . '%,' . $all[$z]['chara_id'] . ',%' . "' ";
			$qx[$z] = Db::query($sql[$z]);
			for ($x = 0; $x < count($qx[$z]); $x++) {
				$qr[$z] .= $qx[$z][$x]['user_name'] . ',';
			}
			$qr[$z] = rtrim($qr[$z], ",");
		}
		foreach ($all as $key => $value) {
			$all[$key]['user_name'] = $qr[$key];
		}
		// dump($qx);die;
		$allchara = Db::table('cn_character')
			->alias('cc')
			->field('chara_name,chara_id')
			->where('is_delete', 'neq', '1')
		// ->where('chara_id','neq','3')
			->select();
		//         dump($allchara);die;
		// for ($a=0; $a < count($allchara); $a++) {
		//     $all[$a] = Db::table('cn_user')
		//             ->alias('cu')
		//             ->join('cn_user_character_relation cucr','cucr.user_id = cu.user_id')
		//             ->where('chara_id','like','%'.$allchara[$a]['chara_id'])
		//             ->select();
		// }

		//获得所有人员 以及对应的组织单位  和  角色
		$renyuan = Db::table('cn_user')
			->alias('cu')
			->join('cn_user_character_relation cucr', 'cu.user_id = cucr.user_id')
		// ->join('cn_character cc','cc.chara_id = cucr.chara_id')
			->join('cn_user_organization_relation cuor', 'cuor.user_id = cu.user_id')
			->join('cn_organization co', 'co.org_id = cuor.org_id')
		// ->where('cu.is_delete', 'eq', '0')
			->where('co.is_delete', 'eq', '0')
			->where('cu.user_name', 'neq', 'admin')
		// ->where('cc.chara_id','neq','3')
			->field('cu.user_name,co.org_name,cu.user_id,cucr.chara_id,cu.is_delete')
		// ->where('co.org_parent_id','eq','0')
		// ->order('cc.chara_id')
		// ->paginate(10);
			->select();
		// dump(explode(",",$renyuan[0]['chara_id']));die;
		for ($i = 0; $i < count($renyuan); $i++) {
			$a = explode(",", $renyuan[$i]['chara_id']);
			$cn[$i] = '';
			for ($j = 0; $j < count($a); $j++) {
				$nn[$i][$j] = Db::table('cn_character')
					->where('chara_id', $a[$j])
					->field('chara_name')
					->find();
				$cn[$i] .= $nn[$i][$j]['chara_name'] . ',';
			}
			$cn[$i] = rtrim($cn[$i], ",");
			// $cn[$i]['chara_name'] = implode(',',$nn[$i]);
		}
		// dump($cn);
		foreach ($renyuan as $key => $value) {
			$renyuan[$key]['chara_name'] = $cn[$key];
		}
		// $page = $renyuan->render();
		// dump($renyuan['']);die;
		// $renyuan = [];
		// 获得所有部门  以及部门的人数(人数没搞定)
		$bumen = Db::table('cn_organization')
			->alias('co')
		// ->join('cn_user_organization_relation cuor','cuor.org_id = co.org_id')
		// ->field('co.org_name,co.org_type,co.org_id,count("co.org_id") as count')
			->field('co.org_name,co.org_type,co.org_id')
			->where('co.org_parent_id = 0')
			->where('co.is_delete = 0')
		// ->where('cuor.is_delete = 0')
		// ->group('co.org_name')
			->select();
		// dump($bumen);die;
		// ->paginate(10);
		// var_dump($bumen);die;
		// $bmpage = $bumen->render();
		// //获取组织结构树
		$org_id = $this->request->get('org_id');
		if (empty($org_id)) {
			$org_id = session('org_id') ? session('org_id') : 1;
		} else {
			session('org_id', $org_id);
		}
		$org_data = $this->org_model->getListByCondition(['is_delete' => 0]);
		$org_tree = recursionCate2($org_data, 'org_id', 0, 'org_parent_id');
		$tree = createCateTree2($org_tree, 'org_id', 'org_parent_id', 'org_name');
		// dump($tree);die;

		$ttt = [
			'node' => $list,
			'cha' => $cha,
			'use' => $username,
			'all' => $all,
			'renyuan' => $renyuan,
			'bumen' => $bumen,
			'tree' => $tree,
			// 'nav_html'=>$nav_html,
			// 'page' => $page,
			'allchara' => $allchara,
			// 'a_data'=>$a_data
			// 'rypage' => $rypage,
			// 'bmpage' => $bmpage
		];
		// var_dump($ttt['use']);die;

		// var_dump($ttt);die;
		// return $ttt;
		return view('index@Manage/index', $ttt);
	}
	// 指向人员修改页面
	public function ryedit($id) {
		$this->getAuth();

		$rev = new Manage_model;
		$res = $rev->getryedit($id);
		// var_dump($bmparent);die;
		return view('index@Manage/rydoEdit', $res);

	}

	// 指向部门修改页面
	public function bmedit($id) {
		$this->getAuth();

		$rev = new Manage_model;
		$res = $rev->getbmedit($id);
		// var_dump($bmparent);die;
		return view('index@Manage/bmdoEdit', $res);
	}

	// 部门修改逻辑
	public function BmdoEdit($id) {
		$rev = new Manage_model;
		$res = $rev->getbmdoedit($id);
		// var_dump($bmparent);die;
		if ($res) {
			$this->success('修改成功', 'manage/index');
		} else {
			$this->error('修改成功', 'manage/index');
		}

	}

	public function ryadd() {
		$this->getAuth();

		$rev = new Manage_model;
		$res = $rev->getryadd();
		// var_dump($res['org']);die;
		return view('index@Manage/rydoAdd', $res);
	}

	public function bmadd() {
		$this->getAuth();

		$rev = new Manage_model;
		$res = $rev->getbmdoadd();

		return view('index@Manage/bmdoAdd', $res);
	}

	public function sqzz() {
		$this->getAuth();

		// dump($_POST);die;
		$rev = new Manage_model;
		$res = $rev->getsqzz();
		// 返回到ajax
		return $res;
	}

	// 跳转到删除逻辑
	public function bmdel($id) {
		$this->getAuth();

		$rev = new Manage_model;
		$res = $rev->getbmdel($id);
		if ($res) {
			$this->success('删除成功', 'manage/index');
		} else {
			$this->error('部门下子部门，请重试', 'manage/index');
		}
	}

	public function rydoEdit() {
		$this->getAuth();

		// dump($_POST);die;
		$rev = new Manage_model;
		$res = $rev->getrydoedit();
		// dump($res);die;
		return $res;

		// var_dump($id);die;

	}

	public function sonindex($id) {
		$rev = new Manage_model;
		$res = $rev->getsonindex($id);
		return view('index@Manage/index', $res);
	}

	public function treeindex($id) {
		$rev = new Manage_model;
		$res = $rev->getsonindex($id);
		return $res;
	}

	public function getZu() {
		$rev = new Manage_model;
		$res = $rev->getZu();
		return $res;
	}

	public function rydel($id) {
		$this->getAuth();

		$rev = new Manage_model;
		$res = $rev->getrydel($id);
		if ($res) {
			$this->success('禁用成功', 'manage/index');
		} else {
			$this->error('禁用失败，请重试', 'manage/index');
		}
	}

	public function ryactive($id) {
		$rev = new Manage_model;
		$res = $rev->getactive($id);
		if ($res) {
			$this->success('激活成功', 'manage/index');
		} else {
			$this->error('激活失败，请重试', 'manage/index');
		}
	}

}